import wx
from zope import interface
import os, sys

APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("must set environment variable SIMPLEWATCHER_INSTALL")
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

from watch_builder.gui.watchobjects.interfaces import IWatchDataObject, IBranchDataObject, IInputQueueDataObject
from watch_builder.gui.watchobjects.base import SimpleWatcherDataObject
from watch_builder.gui import utils

class WatchDataObject(SimpleWatcherDataObject):
    interface.implements(IWatchDataObject)
    """
    data object for dragging and dropping into the workspace for a watch project
    """

    
    def __init__(self, *args, **kwargs):
        super(WatchDataObject, self).__init__(*args, **kwargs)
        self.buildDefaults()
        self.setImage(kwargs.get('image', self._default_image))
        # set all kwarg attrs relevant to building the object's xml
        for (attr, xattr) in self._xml_build_attrs:
            setattr(self, attr, kwargs.get(attr, ''))
        self._children = []
        self.build_schema()
    
    
    def buildDefaults(self):
        # default image
        self._default_image = "watch_object.gif"
        # attributes that can be read to and from xml
        self._xml_build_attrs = [('watchName','//watch/@name'), ('packageName', '//watch/@package' ), ('klass', '//watch/@klass'),
                                 ('startOnInit', '//watch/startOnInit'), ('queueType', '//watch/input_queue/@type'), 
                                 ('queueName', '//watch/input_queue/@name'), ('scheduler', '//watch/watch_schedule/@name'), 
                                 ('branchName','//watch/branch/@name'), ('restBetweenIter', '//watch/rest'), 
                                 ('finalAction', '//watch/final'),]
        
        # valid children, embedded resources, and only child for self.schema
        self._valid_children = [IBranchDataObject]
        self._embedded_resources = [IInputQueueDataObject]
        self._only_child = [IBranchDataObject, IInputQueueDataObject]


    def guiObjXml(self):
        for attr, xattr in self._xml_build_attrs:
            if not hasattr(self, attr):
                setattr(self, attr, '')
            else:
                setattr(self, attr, str(getattr(self, attr)))
        xml = """
        <watch
            name="%(watchName)s"
            package="%(packageName)s"
            klass="%(klass)s"
            >
          <startOnInit>
            <current_value>%(startOnInit)s</current_value>
            <help_text>Whether or not to start this Watch process on server startup</help_text>
            <display_name>Start on Init</display_name>
          </startOnInit>
          <rest>
            <current_value>%(restBetweenIter)s</current_value>
            <help_text>Whether or not to start this Watch process on server startup</help_text>
            <display_name>Rest Between Iterations</display_name>
          </rest>
          <final>
            <current_value>%(finalAction)s</current_value>
            <help_text>Check to clean original file from filesystem after processing</help_text>
            <display_name>Remove Original File</display_name>
          </final>
        </watch>
        """ % self.__dict__
        return xml


    def watchBuilderXml(self):
        for attr, xattr in self._xml_build_attrs:
            if not hasattr(self, attr):
                setattr(self, attr, "")
        xml = """
        <watch
            name="%(watchName)s"
            package="%(packageName)s"
            klass="%(klass)s"
            >
          <startOnInit>%(startOnInit)s</startOnInit>
          <watch_schedule name="%(scheduler)s" />
          <input_queue type="%(queueType)s" name="%(queueName)s"/>
          <branch name="%(branchName)s" />
          <rest>%(restBetweenIter)s</rest>
          <final>%(finalAction)s</final>
        </watch>
        """ % self.__dict__
        return xml

